package com.fira.web.controller.edu;

import java.util.List;

import com.github.pagehelper.util.StringUtil;
import com.fira.common.constant.Constants;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.ShiroUtils;
import com.fira.edu.domain.dto.EduClassroomDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fira.common.annotation.Log;
import com.fira.common.enums.BusinessType;
import com.fira.edu.domain.EduClassroom;
import com.fira.edu.service.IEduClassroomService;
import com.fira.common.core.controller.BaseController;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.utils.poi.ExcelUtil;
import com.fira.common.core.page.TableDataInfo;

/**
 * 教室信息Controller
 * 
 * @author zhushuyong
 * @date 2021-09-29
 */
@Controller
@RequestMapping("/edu/classroom")
public class EduClassroomController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(EduClassroomController.class);
    private String prefix = "edu/classroom";

    @Autowired
    private IEduClassroomService eduClassroomService;

    @RequiresPermissions("edu:classroom:view")
    @GetMapping()
    public String classroom()
    {
        return prefix + "/classroom";
    }

    /**
     * 查询教室信息列表
     */
    @RequiresPermissions("edu:classroom:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EduClassroom eduClassroom)
    {
        startPage();
        List<EduClassroom> list = eduClassroomService.selectEduClassroomList(eduClassroom);
        return getDataTable(list);
    }

    /**
     * 导出教室信息列表
     */
    @RequiresPermissions("edu:classroom:export")
    @Log(title = "教室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EduClassroom eduClassroom)
    {
        List<EduClassroom> list = eduClassroomService.selectEduClassroomList(eduClassroom);
        ExcelUtil<EduClassroom> util = new ExcelUtil<EduClassroom>(EduClassroom.class);
        return util.exportExcel(list, "classroom");
    }

    /**
     * 新增教室信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存教室信息
     */
    @RequiresPermissions("edu:classroom:add")
    @Log(title = "教室信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated EduClassroomDTO classroom)
    {
        String tenantId = ShiroUtils.getTenantId();
        String deptId = ShiroUtils.getSysUser().getDeptId();
        if(StringUtil.isNotEmpty(deptId)){
            classroom.setDeptId(deptId);
        }
        classroom.setTenantId(tenantId);
        classroom.setCreateBy(ShiroUtils.getSysUser().getUserName());
        log.debug("EduClassroomDTO is : {}",classroom);
        return toAjax(eduClassroomService.insertEduClassroom(classroom));
    }

    /**
     * 修改教室信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        EduClassroom eduClassroom = eduClassroomService.selectEduClassroomById(id);
        mmap.put("eduClassroom", eduClassroom);
        return prefix + "/edit";
    }

    /**
     * 修改保存教室信息
     */
    @RequiresPermissions("edu:classroom:edit")
    @Log(title = "教室信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EduClassroom eduClassroom)
    {
        return toAjax(eduClassroomService.updateEduClassroom(eduClassroom));
    }

    /**
     * 删除教室信息
     */
    @RequiresPermissions("edu:classroom:remove")
    @Log(title = "教室信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(eduClassroomService.deleteEduClassroomByIds(ids));
    }
}
